define(["jquery","bbcare","comm/form/form","bch/app/visit/visitAppointAdd.servers"],
	function($,$bb,$form,$servers){
	var visitAppointAdd = {};

	/**
	 * 发卡登记成功后的回调函数
	 */
	var ajaxSubmitPatientRecordCallBack = function(data){
		var params = $bb.getPageParams();
        var domId = $(this).attr("id");
        $bb.open(params,domId,"bch_visitAppointList");
	}

	/**
	 * 发卡登记
	 */
	var onSubmitClick = function(){
		if( $form.validate("visitAppointAddForm") ){
			var params = $(this).data("params");
			var doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
			var channelId = $bb.getChannelId();
	    	var contList = $form.getSaveData("visitAppointAddForm");
	    	var userName = $bb.code.getValueByName("visitAppointAddForm","userName");
	    	if( params.status == "02" ){
	    		var flag = "0";
	    		var id = params.info.id;
	    		var str = "修改";
	    	}else{
	    		var flag = "1";
	    		var id = "";
	    		var str = "新增";
	    	}
	    	var params1 = {
	    		"oprInfo":{
	    			"opCode":"1214",
	    			"opType":"2",
	    			"opNote":"【"+ doctorName +"】医生给【"+ userName +"】患者"+ str +"访视预约信息",
	    			"channelType":"2",
	    			"channelId":channelId,
	    		},
	    		"visitInfo":{
	    			"contList":contList,
	    			"flag":flag,
	    			"id":id,
	    		}
	    	}
	    	$servers.ajaxSubmitPatientRecord(params1,ajaxSubmitPatientRecordCallBack);
    	}
	}

	/**
	 * 请求答案成功后的回调函数
	 */
	var ajaxPatientRecordCallBack = function(data,questions,params){
		var values = data.valueList;
		var status =  params && params.status && params.status == "01" ? "readonly" : "default";
		// 预约状态时，处理数据
		if( params.status == "03" ){
			for (var i = 0; i < values.length; i++) {
				switch(values[i].deCode){
					case $bb.code.getDeCodeByName("visitStage"):
						values[i].quesValue = "2";
						break;
					case $bb.code.getDeCodeByName("visitContactTime"):
						values[i].quesValue = "today";
						break;
					case $bb.code.getDeCodeByName("visitAppointTime"):
						values[i].quesValue = "";
						break;
					case $bb.code.getDeCodeByName("visitAppointTimeHalf"):
						values[i].quesValue = "1";
						break;
				}
			}
		}
		$form.init({
        	id: "visitAppointAddForm",
        	status: status,
            questions: questions,
            values: values,
        });
        if( params.status != "01" ){
        	$("#submitBtnWrap").html('<div id="submitBtn" class="btn btn-green">确定</div>');
	        $("#submitBtn").unbind("click");
	        $("#submitBtn").data("params",params);
	        $("#submitBtn").click(onSubmitClick);
        }
	}

	/**
	 * 请求问卷成功后的回调函数
	 */
	var ajaxQuestionCallBack = function(data,params){
		if( params && params.info && params.info.id ){
			$servers.ajaxPatientRecord(ajaxPatientRecordCallBack,data.rows,params);
		}else{
			ajaxPatientRecordCallBack({},data.rows,params);
		}
	}

	visitAppointAdd.init = function(){
		var params = $bb.getPageParams();
		$servers.getQuestiones(params,ajaxQuestionCallBack);
	}

	return visitAppointAdd;
});